﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using Visuals.Wizard;

namespace Visuals
{
    /// <summary>
    /// Interaction logic for StartWindow.xaml
    /// </summary>
    public partial class StartWindow : Window
    {
        public bool WizardSetting { get; set; }

        public StartWindow()
        {
            MainWindow.Object.CloseStart = false;
            if(Application.Current.Properties.Contains("ProjectFilePath"))
            {
                Dispatcher.Invoke(DispatcherPriority.Normal, new Action(
                                                                 () =>
                                                                 MainWindow.Object.OpenCorpus(
                                                                     (string)
                                                                     Application.Current.Properties["ProjectFilePath"])));
                Close();
            }

            InitializeComponent();
            WizardSetting = Properties.Settings.Default.ShowWizard;
            DataContext = this;
        }

        private void NewProjectGrid_MouseEnter(object sender, MouseEventArgs e)
        {
            (newProjectGrid.Children[0]).SetValue(MarginProperty, new Thickness(0,-2,0,29));
            newProjectGrid.Cursor = Cursors.Hand;
        }

        private void OpenProjectGrid_MouseEnter(object sender, MouseEventArgs e)
        {
            (openProjectGrid.Children[0]).SetValue(MarginProperty, new Thickness(0, -2, 5, 29));
            openProjectGrid.Cursor = Cursors.Hand;
        }

        private void NewProjectGrid_MouseLeave(object sender, MouseEventArgs e)
        {
            (newProjectGrid.Children[0]).SetValue(MarginProperty, new Thickness(0, 0, 0, 27));
            newProjectGrid.Cursor = Cursors.Arrow;
        }

        private void OpenProjectGrid_MouseLeave(object sender, MouseEventArgs e)
        {
            (openProjectGrid.Children[0]).SetValue(MarginProperty, new Thickness(0, 0, 5, 27));
            openProjectGrid.Cursor = Cursors.Arrow;
        }

        private void openProjectGrid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => MainWindow.Object.OpenProject()));
            if (MainWindow.Object.CloseStart)
                Close();
        }

        private void newProjectGrid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => MainWindow.Object.LoadCorpus()));
            if (MainWindow.Object.fileNames == null)
                return;
            if (MainWindow.Object.fileNames.Length < 2)
            {
                MessageBox.Show("Трябва да изберете поне два файла.");
                return;
            }

            if (checkBoxUseWizard.IsChecked == true)
            {
                FirstStepWindow firstStep = new FirstStepWindow(MainWindow.Object.fileNames.Length);
                Close();
                firstStep.ShowDialog();
            }
            else
            {
                Close();
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Properties.Settings.Default.ShowWizard = checkBoxUseWizard.IsChecked == true;
            Properties.Settings.Default.Save();
        }   
    }
}
